package com.gaomall.dubbo.service.user.provider.api.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.gaomall.dubbo.commons.domain.TbUser;
import com.gaomall.dubbo.commons.mapper.TbUserMapper;
import com.gaomall.dubbo.redis.api.RedisService;
import com.gaomall.dubbo.service.user.api.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service(version = "${services.versions.user.v1}")
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {

    @Autowired
    private TbUserMapper userMapper;

    @Value("${dubbo.protocol.port}")
    private String port;

//    @Reference(version = "${services.versions.redis.v1}");
    private RedisService redisService;

    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
    })
    @Override
    public List<TbUser> selectAll() {
        return userMapper.selectAll();
//        return "Hello Dubbo, i am from port:" + port;
//        throw new RuntimeException("Exception to show hystrix enabled.");
    }

    @Override
    public PageInfo<TbUser> page(int pageNum, int pageSize) {

//        RedisService.set('test', "123");


        Example example = new Example(TbUser.class);
        PageHelper.startPage(pageNum, pageSize);
//        PageInfo<TbUser> pageInfo = PageInfo<>(TbUserMapper.selectByExample(example));
        return null;
    }

}
